//
//  Utility.swift
//  App
//
//  Created by melvyn on 2018/8/31.
//

import Foundation
import Vapor


let imageURL = DirectoryConfig.detect().workDir + "/Public/images"
let avatarURL = DirectoryConfig.detect().workDir + "/Public/images/avatars"

class Utility {
    static let `default`: Utility = Utility()
    
    func save(_ file: Data?, name: CustomStringConvertible = UUID().uuidString,
              destination: String = DirectoryConfig.detect().workDir,
              options: Data.WritingOptions = []) throws -> (String, URL) {
        
        guard let metadata = file else {
            throw ALError.dataFileNil
        }
        
        var saveURL = URL(fileURLWithPath: destination)
        
        if !FileManager.default.fileExists(atPath: saveURL.path) {
            try FileManager.default.createDirectory(atPath: saveURL.path, withIntermediateDirectories: true, attributes: nil)
        }
        
        saveURL = saveURL.appendingPathComponent(name.description)
        
        try metadata.write(to: saveURL, options: options)
        
        return (name.description, saveURL)
    }
}

